package edu.washington.ling.syntric.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

import edu.washington.ling.syntric.util.LaTeXUtils;
import edu.washington.ling.syntric.util.Utils;

public class RuleFileWriterLaTeX extends RuleAndExampleFileWriter {

	private PrintStream pStream;
	
	public RuleFileWriterLaTeX(String path) throws IOException {
		super(path);
		try {
			pStream = new PrintStream(new FileOutputStream(new File(path)), true, "UTF8");
		} 
		catch(UnsupportedEncodingException e) {
			pStream = new PrintStream(new FileOutputStream(new File(path)), true);
		}
		pStream.print(LaTeXUtils.formatPreamble("Tree Output"));
		pStream.println("\\begin{enumerate}");
	}
	private List<String> getFormattedIDs(List<Example> exrs) {
		List<String> ids = new LinkedList<String>();
		for (Example exr: exrs) {
			ids.add(LaTeXUtils.formatForLabel(exr.getID()));
		}
		return ids;
	}

	@Override
	public void close() {
		pStream.println("\\end{enumerate}");
		pStream.println("\\end{document}");
		pStream.flush();
		pStream.close();
	}
	public void writeTreeRep(TreeRepresentation tr) {
		pStream.println(String.format("\\begin{item}[]", tr.getLayer()));
		pStream.println(String.format("\\tikz[baseline]\\node[anchor=base,draw=black]{\\bf %s}; ", tr.getLayer()));
		for (String metaData: tr.getMetaData()) {
			pStream.println(String.format("\\tikz[baseline]\\node[anchor=base,draw=red]{\\texttt{%s}}; ", metaData));
		}
		pStream.println(String.format("\\end{item}", tr.getLayer()));
		pStream.println(String.format("\\begin{item}[]", tr.getLayer()));
		for(String line: tr.getTreeLines()) {
			pStream.println(line);
		}
		pStream.println("\\end{item}");
	}
	@Override
	public void writeHeaderForAmbigRule(String dsPattStr, int total) {
		pStream.println(String.format("\\item {\\bf Ambiguous DS Pattern:} %s (%d rules)", dsPattStr, total));
		pStream.println("\\begin{enumerate}");
	}
	@Override
	public void writeFooterForAmbigRule() {
		pStream.println("\\end{enumerate}");
	}
	@Override
	public void writeRuleAndExampleReps(RuleRepresentation rr, List<Example> exrs, String[] includeLayers) {
		writeRuleAndExampleReps(rr, exrs, includeLayers, exrs.size());
	}
	@Override
	public void writeRuleAndExampleReps(RuleRepresentation rr, List<Example> exrs, String[] includeLayers, int writeLimit) {
		pStream.println("\\begin{item}");
		String ruleStr = String.format("%s $\\rightarrow$ %s", rr.getDSPattStr(), rr.getPSPattStr());
		pStream.println(String.format("{\\bf Rule:} %s (%d tree pairs)\\\\", ruleStr, exrs.size()));
		pStream.println(String.format("Occurs in: %s", 
				Utils.join(getFormattedIDs(exrs), ", ")));
		pStream.println("\\begin{itemize}");
		int writeCount = 0;
		for (Example exr: exrs) {
			if (writeCount >= writeLimit) break;
			LaTeXUtils.writeExampleRep(exr, includeLayers, pStream);
			writeCount++;
		}
		pStream.println("\\end{itemize}");
		pStream.println("\\end{item}");
	}
	@Override
	public void writeRuleRep(RuleRepresentation rr, int count) {
		pStream.println("\\begin{item}");
		String ruleStr = String.format("%s $\\rightarrow$ %s", rr.getDSPattStr(), rr.getPSPattStr());
		pStream.println(String.format("Rule: %s; count=%d\\\\", ruleStr, count));
		pStream.println("\\end{item}");
	}
}
